/**
 * Deteksi Plagiasi Dokumen
 *
 * @author Anung B. Ariwibowo
 * @date 20120325
 */

import java.io.File;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.IOException;
import java.util.*;
import java.util.TreeSet;
import org.apache.tika.Tika;

public class SiapGila {
  public static void main(String[] args) throws Exception {
    FindFile fileFinder = new FindFile();
    Extractor extractor = new Extractor();
    Tika tika = new Tika();
    TreeSet<String> setType = new TreeSet();
    TreeSet<String> setResult = new TreeSet();

    File folder = new File(args[0]);
    String searchTerm = new String(args[1]);
    fileFinder.getFiles(folder);
    List<File> list = fileFinder.getFileList();

    System.out.println("File(s) found: " + list.size());
    System.out.println("Searching term '" + searchTerm + "' ...");

    for (File file : list) {
      String fName = file.getName().toLowerCase();
      String type = tika.detect(file);
      setType.add(type);
      if (fName.endsWith(".pdf")) {
        if (extractor.searchContent(file, searchTerm)) {
          setResult.add(fName);
        }
      }
      /*
      if (fName.endsWith(".pdf")) {
        extractor.getContent(file);
      }
       */
    }
    /*
application/java-vm
application/msword
application/octet-stream
application/pdf
application/postscript
application/rtf
application/vnd.ms-excel
application/vnd.ms-powerpoint
application/vnd.oasis.opendocument.spreadsheet
application/vnd.oasis.opendocument.text
application/vnd.oasis.opendocument.text-master
application/vnd.openxmlformats-officedocument.presentationml.presentation
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
application/vnd.openxmlformats-officedocument.wordprocessingml.document
application/x-gzip
application/x-msaccess
application/xhtml+xml
image/png
text/calendar
text/csv
text/html
text/plain
text/x-java-source
text/x-vcalendar
     */
    if (setResult.size() > 0) {
      int docCtr = 1;
      System.out.println(searchTerm + " found in file(s):");
      for (String aDocument : setResult) {
        System.out.println(docCtr + " " + aDocument);
        docCtr++;
      }
    }
    int typeCtr = 1;
    System.out.println("Type(s) of files found:");
    for (String aType : setType) {
      System.out.println(typeCtr + " " + aType);
      typeCtr++;
    }
  }
}

